//
//  UpdatePassViewController.m
//  SHHCPhone
//
//  Created by sheng long on 14-6-29.
//  Copyright (c) 2014年 sheng long. All rights reserved.
//

#import "UpdatePassViewController.h"
#import "TextFieldView.h"

@interface UpdatePassViewController ()<UITextFieldDelegate>
{
    TextFieldView *fieldView1;
    TextFieldView *fieldView2;
    TextFieldView *fieldView3;
}
@end

@implementation UpdatePassViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [navigationBarView setTitle:@"修改密码"];
    [navigationBarView setNormalLeftButton];

    UIButton *bgButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [bgButton addTarget:self action:@selector(closeKeyboard) forControlEvents:UIControlEventTouchUpInside];
    [bgButton setFrame:self.view.bounds];
    [self.view insertSubview:bgButton belowSubview:navigationBarView];
    
    fieldView1 = [[TextFieldView alloc] init];
    [self.view addSubview:fieldView1];
    
    fieldView2 = [[TextFieldView alloc] init];
    [self.view addSubview:fieldView2];
    
    fieldView3 = [[TextFieldView alloc] init];
    [self.view addSubview:fieldView3];
    
    [fieldView1 setBgImage:@"输入框-1.png" titleImage:nil tip:@"请输入旧密码"];
    fieldView1.textField.keyboardType = UIKeyboardTypeASCIICapable;
    fieldView1.textField.returnKeyType = UIReturnKeyNext;
    [fieldView1.textField setSecureTextEntry:YES];
    [fieldView1.textField addTarget:self action:@selector(nextButt:) forControlEvents:UIControlEventEditingDidEndOnExit];
    
    [fieldView2 setBgImage:@"输入框-1.png" titleImage:nil tip:@"请输入新密码"];
    fieldView2.textField.keyboardType = UIKeyboardTypeASCIICapable;
    fieldView2.textField.returnKeyType = UIReturnKeyNext;
    [fieldView2.textField setSecureTextEntry:YES];
    [fieldView2.textField addTarget:self action:@selector(nextButt:) forControlEvents:UIControlEventEditingDidEndOnExit];
    
    [fieldView3 setBgImage:@"输入框-1.png" titleImage:nil tip:@"再次输入新密码"];
    fieldView3.textField.keyboardType = UIKeyboardTypeASCIICapable;
    fieldView3.textField.returnKeyType = UIReturnKeyDone;
    [fieldView3.textField setSecureTextEntry:YES];
    [fieldView3.textField addTarget:self action:@selector(nextButt:) forControlEvents:UIControlEventEditingDidEndOnExit];

    [fieldView1 setCenter:CGPointMake(self.view.frame.size.width/2, NAVIGATIONBAR_HEIGHT+25+fieldView1.frame.size.height/2)];
    [fieldView2 setCenter:CGPointMake(self.view.frame.size.width/2, 19+fieldView1.frame.size.height+fieldView1.frame.origin.y+fieldView2.frame.size.height/2)];
    [fieldView3 setCenter:CGPointMake(self.view.frame.size.width/2, 19+fieldView2.frame.size.height+fieldView2.frame.origin.y+fieldView3.frame.size.height/2)];
    
    [fieldView1.textField setDelegate:self];
    [fieldView2.textField setDelegate:self];
    [fieldView3.textField setDelegate:self];
    
    
    UIButton *button1 = [OftenMenthod setButtonTitle:@"确定"];
    [self.view addSubview:button1];
    [button1 addTarget:self action:@selector(doneButt) forControlEvents:UIControlEventTouchUpInside];
    [button1 setCenter:CGPointMake(fieldView3.center.x, fieldView3.frame.size.height+fieldView3.frame.origin.y+46+button1.frame.size.height/2)];
    
}

//关闭键盘
-(void)closeKeyboard
{
    [fieldView1.textField resignFirstResponder];
    [fieldView2.textField resignFirstResponder];
    [fieldView3.textField resignFirstResponder];
}

-(void)doneButt
{
    if (fieldView1.textField.text.length==0) {
        [LoadingFrameView autoDisappearTipview:@"请输入旧密码"];
    }else if (fieldView2.textField.text.length==0){
        [LoadingFrameView autoDisappearTipview:@"请输入新密码"];
    }else if (fieldView3.textField.text.length==0){
        [LoadingFrameView autoDisappearTipview:@"请再次输入新密码"];
    }else if (![fieldView2.textField.text isEqualToString:fieldView3.textField.text]){
        [LoadingFrameView autoDisappearTipview:@"确认密码不匹配"];
    }else if (![APP_USER_MSG.password isEqualToString:fieldView1.textField.text]){
        [LoadingFrameView autoDisappearTipview:@"旧密码不正确"];
    }else if ([fieldView2.textField.text isEqualToString:fieldView1.textField.text]){
        [LoadingFrameView autoDisappearTipview:@"修改后密码与旧密码一致"];
    }else{
        [self closeKeyboard];
        [LoadingFrameView showLoadingview];
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:1];
        [dic setValue:IOS_CHANGEPASSWORD forKeyPath:IOS_METHOD_NAME];
        [dic setValue:APP_USER_MSG.id forKey:@"id"];
        [dic setValue:fieldView1.textField.text forKey:@"password"];
        [dic setValue:fieldView2.textField.text forKey:@"newPassword"];
        
        ASIHTTPRequest *request =  [NetUtls httpGet:self withValue:dic tag:IOS_CHANGEPASSWORD_TAG need:YES];
        [self.netArray addObject:request];
        [self superASIStartAsynchronous:request];
    }
}

-(void)nextButt:(UITextField*)textField
{
    if ([textField isEqual:fieldView1.textField]) {
        [fieldView2.textField becomeFirstResponder];
    }else if([textField isEqual:fieldView2.textField]){
        [fieldView3.textField becomeFirstResponder];
    }else{
        [self closeKeyboard];
    }
}

#pragma mark - ASIHTTPRequestDelegate
-(void)requestFinished:(ASIHTTPRequest *)request
{
    
}

-(void)requestFailed:(ASIHTTPRequest *)request
{
    [LoadingFrameView autoDisappearBlockingview:@"修改密码失败"];
}

//父类获取服务器返回信息，成功获取到数据执行方法
-(void)getASIRecordSucceed:(ASIHTTPRequest *)request data:(NSDictionary*)dic
{
    //保存信息
    NSString *user = [APP_USERDEFAULTS valueForKey:APP_LOGIN_SUCESS_SVAE_MSG];
    NSMutableDictionary *d = [NSMutableDictionary dictionaryWithDictionary:[user objectFromJSONString]];
    [d setValue:fieldView2.textField.text forKey:@"password"];
    [APP_USERDEFAULTS setValue:[d JSONString] forKey:APP_LOGIN_SUCESS_SVAE_MSG];
    
    //保存密码
    APP_USER_MSG.password = fieldView2.textField.text;
    
    [LoadingFrameView autoDisappearTipview:@"修改密码成功"];
    [self leftButtonClick];
}

//父类获取服务器返回信息，未成功获取到数据执行方法
-(void)getASIRecordFailed:(ASIHTTPRequest *)request data:(NSString*)failed
{
    [LoadingFrameView autoDisappearBlockingview:failed];
}


@end
